package it.uniroma3.dia.digraph.view;

import it.uniroma3.dia.digraph.algorithm.utility.Utility;
import it.uniroma3.dia.digraph.algorithm.utility.kouterplanar.Peeler;
import it.uniroma3.dia.digraph.algorithm.utility.polygon.GeneralPolygon;
import it.uniroma3.dia.digraph.io.util.IoManager;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.graphdrawing.graphml.xmlns.GraphType;
import org.graphdrawing.graphml.xmlns.NodeType;
import org.graphdrawing.graphml.xmlns.ObjectFactory;


public class Main {
	
	

	
	public static void main(String[] args) throws IOException{
		IoManager iom = new IoManager();
		GraphType graph = iom.loadFromFile("xml\\kouterplanar\\2outerplanar.xml").getGraph();
		Map<String, Set<String>> adjMatrix = Utility.buildAdjacencyMatrix(graph);
		Map<String, NodeType> nodeMap = Utility.extractNodes(graph);
		List<NodeType> nodes = new LinkedList<NodeType>();
		nodes.addAll(graph.getNodeCollection().getNode());
		List<NodeType> boundary = Peeler.getBoundaryNodes(nodes, adjMatrix, nodeMap);
//		System.out.println("end");
//		for (NodeType n : boundary){
//			System.out.print(n+" ");
//		}
		
	}
	
	public static void mainLayers(String[] args) throws IOException{
		IoManager iom = new IoManager();
		GraphType graph = iom.loadFromFile("xml\\kouterplanar\\2outerplanar.xml").getGraph();
		Map<Integer, List<NodeType>> layers = Peeler.decomposeToLayers(graph);
		
		for (Integer d : layers.keySet()){
			System.out.println("Depth: "+d.intValue());
			for (NodeType n : layers.get(d)){
				System.out.print(n+" ");
			}
			System.out.println();
		}
	}
	
	public static void mainMean (String[] args){
		NodeType a,b,c,d,e,f,x;
		ObjectFactory of = new ObjectFactory();
		
		a = of.createNodeType(); a.setId("a");
		b = of.createNodeType(); b.setId("b");
		c = of.createNodeType(); c.setId("c");
		d = of.createNodeType(); d.setId("d");
		e = of.createNodeType(); e.setId("e");
		f = of.createNodeType(); f.setId("f");
		x = of.createNodeType(); x.setId("x");
		
		a.setX(2); a.setY(1);
		b.setX(3); b.setY(2);
		c.setX(3); c.setY(4);
		d.setX(2); d.setY(3);
		e.setX(1); e.setY(4);
		f.setX(1); f.setY(2);
		x.setX(2); x.setY(2);
		
		List<NodeType> boundary = new LinkedList<NodeType>();
		boundary.add(a);
		boundary.add(b);
		boundary.add(c);
		boundary.add(d);
		boundary.add(e);
		boundary.add(f);
		
		
		Map<NodeType, Double> lambdas = GeneralPolygon.getMeanValueCoordinates(x, boundary);
		System.out.println("area: "+GeneralPolygon.signedArea(boundary));
		for (NodeType n : lambdas.keySet()){
			System.out.println(n+"->"+lambdas.get(n));
		}
		
		System.out.println("move 1 to right");
		a.setX(a.getX()+1); b.setX(b.getX()+1); c.setX(c.getX()+1); d.setX(d.getX()+1);e.setX(e.getX()+1);f.setX(f.getX()+1);
		
		double xx = 0;
		for (NodeType n : lambdas.keySet()){
			xx+= n.getX()* lambdas.get(n);
		}
		
		System.out.println(xx);
		
		
	}
}
